//
//  GYActivityViewController.m
//  GoodLuck
//
//  Created by apple on 2018/4/25.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYActivityViewController.h"
#import "GYNetWorkTools.h"

@interface GYActivityViewController ()<UIWebViewDelegate>

/**  */
@property (nonatomic, strong) UIWebView *webView;
@end

@implementation GYActivityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

- (void)setupUI {
    self.navigationItem.title = @"活动专题";
    
    //初始webView
    UIWebView *webView =[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-64)];
    webView.backgroundColor = [UIColor clearColor];
    webView.opaque = NO;
    webView.delegate = self;
    
    [self.view addSubview:webView];
    self.webView = webView;
    
    //加载活动地址
    [GYNetWorkTools requestActivityInfo:^(NSDictionary *obj) {
//        NSLog(@"obj=========%@",obj);
        int code = [[obj objectForKey:@"code"] intValue];
        if (code == 200) {
            NSString *datas = [obj objectForKey:@"datas"];
            NSURL *url = [NSURL URLWithString:datas];
            NSURLRequest *request = [NSURLRequest requestWithURL:url];
            [webView loadRequest:request];
        }else {
            [EasyTextView showText:@"亲，您的手机网络不太顺畅喔..."];
        }
    } error:^(NSError *error) {
        [EasyTextView showText:@"亲，您的手机网络不太顺畅喔..."];
        NSLog(@"loadActivityInfo===%@",error);
    }];
}

- (void)back {
    //回退到上个网页
    [self.webView goBack];
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    [EasyLodingView showLodingText:@"正在加载..."];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [EasyLodingView hidenLoding];
    //当网页加载完成后 判断是否可以后退
    if (webView.canGoBack) {
        self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"arrow_left_b" tagret:self action:@selector(back) button:nil];
    }else {
        self.navigationItem.leftBarButtonItem  = nil;
    }
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSLog(@"url===========%@",request.URL.description);

    return YES;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    NSLog(@"加载失败...");
    [EasyLodingView hidenLoding];
    [EasyTextView showText:@"亲，您的手机网络不太顺畅喔..."];
    if (webView.canGoBack) {
        self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:@"arrow_left_b" tagret:self action:@selector(back) button:nil];
    }else {
        self.navigationItem.leftBarButtonItem  = nil;
    }
}
@end
